﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Module.Category.Properties;
using QLBH.DTO;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.Base;

namespace Module.Category
{
    public partial class AddEditDeptDlg : Form
    {
        private eDialogType dialogType=eDialogType.DL_ADD;

        public eDialogType DialogType
        {
            get { return dialogType; }
            set 
            { 
                dialogType = value;
                if (dialogType == eDialogType.DL_ADD)
                {
                    this.Text = "Thêm phòng ban";
                    this.Icon = Icon.FromHandle(Resources.add_24.GetHicon());
                }
                else
                {
                    this.Text = "Sửa thông tin phòng ban";
                    this.Icon = Icon.FromHandle(Resources.write_24.GetHicon());
                }
            }
        }
        private PhongBanDTO newDept;

        public PhongBanDTO NewDept
        {
            get { return newDept; }
            set 
            { 
                newDept = value;
                if (dialogType==eDialogType.DL_EDIT)
                {
                    textBoxTenPhong.Text = newDept.TenPhong;
                    textBoxGhiChu.Text = newDept.GhiChu;
                }
            }
        }
        public AddEditDeptDlg()
        {
            InitializeComponent();
            newDept = new PhongBanDTO();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (IsValidValue())
            {
                try
                {
                    if (dialogType == eDialogType.DL_ADD)
                    {
                        AddNewDept();
                    }
                    else
                    {
                        UpdateDept();
                    }
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxTenPhong.SelectAll();
                    textBoxTenPhong.Focus();
                }                
            }            
        }

        private bool IsValidValue()
        {
            //DeptName 
            try
            {
                DataInputHelper.CheckTextData(textBoxTenPhong.Text, "Tên phòng ban");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxTenPhong.SelectAll();
                textBoxTenPhong.Focus();
                return false;
            }
            return true;
        }

        private void UpdateDept()
        {
            UpdateDataFromControl();
            DeptController.Instance.Edit(newDept.ToEntity());
        }

        private void AddNewDept()
        {
            UpdateDataFromControl();
            newDept=DeptController.Instance.Add(newDept.ToEntity()).ToDTO();
        }
        private void UpdateDataFromControl()
        {
            CuaHang currentShop = ShopController.Instance.GetAll().FirstOrDefault();
            if (currentShop != null)
            {
                newDept.GhiChu = textBoxGhiChu.Text;
                newDept.TenPhong = textBoxTenPhong.Text;
                newDept.CuaHangId = currentShop.Id;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AddEditDeptDlg_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
    }
}
